'use strict';

var mongoose = require('mongoose');

var productModel = function () {

    //Define a super simple schema for our products.
    var productSchema = mongoose.Schema({
        item_id: Number,
        name: String,
        user_name: String,
        company: String,
        brand: String,
        place: String,
        price: Number,
        qty: Number,
        packing: String,
        type_id: Number,
        cat_id: Number,
        spec: String,
        tag: String,
        keyword: String,
        hits: Number,
        orders: Number,
        sells: Number,
        Thumb: String,
        Thumb1: String,
        Thumb2: String,
        Thumb3: String,
        Thumb4: String,
        taobao_url: String,
        intro: String
    });

    //Verbose toString method
    productSchema.methods.whatAmI = function () {
        var greeting = this.user_name ?
            'Hello, I\'m a ' + this.user_name + ' and I\'m worth ' + this.prettyPrice()
            : 'I don\'t have a name :(';
    };

    //Format the price of the product to show a dollar sign, and two decimal places
    productSchema.methods.prettyPrice = function () {
        return (this && this.price) ? '$' + this.price.toFixed(2) : '$';
    };

    productSchema.methods.getProductCount = function () {
        return this.length;
    };

    return mongoose.model('Product', productSchema);

};

module.exports = new productModel();
